/*
 * Teknei 2014 - All rights reserved
 */
package com.teknei.acabus.example;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.teknei.acabus.FacadeException;
import com.teknei.acabus.core.entities.TableA;
import com.teknei.acabus.core.entities.TableB;
import com.teknei.acabus.dto.TableCDTO;
import com.teknei.acabus.example.common.MessageConstants;
import com.teknei.acabus.example.facade.TableFacade;

/**
 * Controller for TableC elements
 * @author Jorge Amaro
 * @version 1.0
 * @since 1.0
 *
 */
@ManagedBean(name="tc_controller")
@ViewScoped
public class TableCController extends GenericController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(TableCController.class);
	
	@EJB
	private TableFacade facadeTables;
	private String input_edit_dsc;
	private String input_edit_val;
	private String input_new_dsc;
	private String input_new_val;
	private List<TableCDTO> itemsList;
	private TableCDTO currentItem;
	private TableCDTO newItem;
	private int input_edit_idTableA;
	private int input_edit_idTableB;
	private int input_new_idTableA;
	private int input_new_idTableB;
	private List<TableA> itemsListTableA;
	private List<TableB> itemsListTableB;
	
	/**
	 * PostConstruct method. Initialize items
	 */
	@PostConstruct
	public void postConstruct(){
		initItemsList();
	}
	
	/**
	 * Initialize items
	 */
	private void initItemsList(){
		if(CollectionUtils.isEmpty(itemsList)){
			try {
				itemsList = facadeTables.findAllTableCDTO();
			} catch (FacadeException e) {
				super.addErrorMessage(MessageConstants.ACTION_ERROR_INIT_LIST);
			}
		}
	}
	
	/**
	 * Initialize items for TableA
	 */
	private void initItemsListTableA(){
		if(CollectionUtils.isEmpty(itemsListTableA)){
			try {
				itemsListTableA = facadeTables.findAllTableA();
			} catch (FacadeException e) {
				super.addErrorMessage(MessageConstants.ACTION_ERROR_INIT_LIST);
			}
		}
	}
	
	/**
	 * Initialize items for TableB
	 */
	private void initItemsListTableB(){
		if(CollectionUtils.isEmpty(itemsListTableB)){
			try {
				itemsListTableB = facadeTables.findAllTableB();
			} catch (FacadeException e) {
				super.addErrorMessage(MessageConstants.ACTION_ERROR_INIT_LIST);
			}
		}
	}
	
	/**
	 * Refresh items lists
	 */
	private void refreshItemsList(){
		itemsList = null;
		initItemsList();
		currentItem = null;
		newItem = null;
	}
	
	/**
	 * Refresh inputs
	 */
	private void refreshInputs(){
		input_edit_dsc = "";
		input_edit_val = "";
		input_new_dsc = "";
		input_new_val = "";
	}
	
	/**
	 * Saves the instance
	 */
	public void onSaveTableC(){
		newItem = new TableCDTO();
		newItem.setDescription(input_new_dsc);
		newItem.setValue(input_new_val);
		newItem.setIdTableA(input_new_idTableA);
		newItem.setIdTableB(input_new_idTableB);
		try {
			int pk = facadeTables.saveTableC(newItem);
			newItem.setIdField(pk);
			itemsList.add(newItem);
			newItem = null;
			refreshInputs();
			super.addInfoMessage(MessageConstants.ACTION_SAVE_ITEM_SUCCESS);
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_SAVE_ITEM_ERROR);
		}
	}
	
	/**
	 * Cancels the save and refresh inputs
	 */
	public void onSaveCancelTableC(){
		refreshInputs();
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}
	
	/**
	 * Updates the instance
	 */
	public void onEditTableC(){
		boolean changes = false;
		if(input_edit_dsc != null && input_edit_dsc.trim().length() > 1){
			currentItem.setDescription(input_edit_dsc);
			changes = true;
		}
		if(input_edit_val != null && input_edit_val.trim().length() > 1){
			currentItem.setValue(input_edit_val);
			changes = true;
		}
		if(input_edit_idTableA != 0){
			currentItem.setIdTableA(input_edit_idTableA);
			changes = true;
		}
		if(input_edit_idTableB != 0){
			currentItem.setIdTableB(input_edit_idTableB);
			changes = true;
		}
		if(!changes){
			super.addWarningMessage(MessageConstants.WARNING_EMPTY_UPDATE_FIELDS);
			return;
		}
		try {
			facadeTables.updateTableC(currentItem);
			refreshItemsList();
			super.addInfoMessage(MessageConstants.ACTION_UPDATE_SUCCESSFUL);
			currentItem = null;
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_UPDATE_FAILURE);
		}
	}
	
	/**
	 * Cancels the update and refresh inputs
	 */
	public void onEditCancelTableC(){
		refreshInputs();
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}
	
	
	/**
	 * Delete instance
	 */
	public void onDeleteTableC(){
		try {
			facadeTables.deleteTableC(currentItem);
			refreshItemsList();
			super.addInfoMessage(MessageConstants.ACTION_DELETE_ITEM_SUCCESS, MessageConstants.MESSAGE_DELETE_ERROR_HINT);
		} catch (FacadeException e) {
			refreshItemsList();
			super.addErrorMessage(MessageConstants.ACTION_DELETE_ITEM_ERROR, MessageConstants.MESSAGE_DELETE_ERROR_HINT);
		}
	}
	
	/**
	 * Cancels delete
	 */
	public void onDeleteCancelTableC(){
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}

	
	
	/**
	 * @return the facadeTables
	 */
	public TableFacade getFacadeTables() {
		return facadeTables;
	}
	/**
	 * @param facadeTables the facadeTables to set
	 */
	public void setFacadeTables(TableFacade facadeTables) {
		this.facadeTables = facadeTables;
	}
	/**
	 * @return the input_edit_dsc
	 */
	public String getInput_edit_dsc() {
		return input_edit_dsc;
	}
	/**
	 * @param input_edit_dsc the input_edit_dsc to set
	 */
	public void setInput_edit_dsc(String input_edit_dsc) {
		this.input_edit_dsc = input_edit_dsc;
	}
	/**
	 * @return the input_edit_val
	 */
	public String getInput_edit_val() {
		return input_edit_val;
	}
	/**
	 * @param input_edit_val the input_edit_val to set
	 */
	public void setInput_edit_val(String input_edit_val) {
		this.input_edit_val = input_edit_val;
	}
	/**
	 * @return the input_new_dsc
	 */
	public String getInput_new_dsc() {
		return input_new_dsc;
	}
	/**
	 * @param input_new_dsc the input_new_dsc to set
	 */
	public void setInput_new_dsc(String input_new_dsc) {
		this.input_new_dsc = input_new_dsc;
	}
	/**
	 * @return the input_new_val
	 */
	public String getInput_new_val() {
		return input_new_val;
	}
	/**
	 * @param input_new_val the input_new_val to set
	 */
	public void setInput_new_val(String input_new_val) {
		this.input_new_val = input_new_val;
	}
	/**
	 * @return the itemsList
	 */
	public List<TableCDTO> getItemsList() {
		initItemsList();
		return itemsList;
	}
	/**
	 * @param itemsList the itemsList to set
	 */
	public void setItemsList(List<TableCDTO> itemsList) {
		this.itemsList = itemsList;
	}

	/**
	 * @return the currentItem
	 */
	public TableCDTO getCurrentItem() {
		return currentItem;
	}

	/**
	 * @param currentItem the currentItem to set
	 */
	public void setCurrentItem(TableCDTO currentItem) {
		this.currentItem = currentItem;
	}

	/**
	 * @return the newItem
	 */
	public TableCDTO getNewItem() {
		return newItem;
	}

	/**
	 * @param newItem the newItem to set
	 */
	public void setNewItem(TableCDTO newItem) {
		this.newItem = newItem;
	}

	/**
	 * @return the input_edit_idTableA
	 */
	public int getInput_edit_idTableA() {
		return input_edit_idTableA;
	}

	/**
	 * @param input_edit_idTableA the input_edit_idTableA to set
	 */
	public void setInput_edit_idTableA(int input_edit_idTableA) {
		this.input_edit_idTableA = input_edit_idTableA;
	}

	/**
	 * @return the input_edit_idTableB
	 */
	public int getInput_edit_idTableB() {
		return input_edit_idTableB;
	}

	/**
	 * @param input_edit_idTableB the input_edit_idTableB to set
	 */
	public void setInput_edit_idTableB(int input_edit_idTableB) {
		this.input_edit_idTableB = input_edit_idTableB;
	}

	/**
	 * @return the input_new_idTableA
	 */
	public int getInput_new_idTableA() {
		return input_new_idTableA;
	}

	/**
	 * @param input_new_idTableA the input_new_idTableA to set
	 */
	public void setInput_new_idTableA(int input_new_idTableA) {
		this.input_new_idTableA = input_new_idTableA;
	}

	/**
	 * @return the input_new_idTableB
	 */
	public int getInput_new_idTableB() {
		return input_new_idTableB;
	}

	/**
	 * @param input_new_idTableB the input_new_idTableB to set
	 */
	public void setInput_new_idTableB(int input_new_idTableB) {
		this.input_new_idTableB = input_new_idTableB;
	}

	/**
	 * @return the itemsListTableA
	 */
	public List<TableA> getItemsListTableA() {
		initItemsListTableA();
		return itemsListTableA;
	}

	/**
	 * @param itemsListTableA the itemsListTableA to set
	 */
	public void setItemsListTableA(List<TableA> itemsListTableA) {
		this.itemsListTableA = itemsListTableA;
	}

	/**
	 * @return the itemsListTableB
	 */
	public List<TableB> getItemsListTableB() {
		initItemsListTableB();
		return itemsListTableB;
	}

	/**
	 * @param itemsListTableB the itemsListTableB to set
	 */
	public void setItemsListTableB(List<TableB> itemsListTableB) {
		this.itemsListTableB = itemsListTableB;
	}
	
	
	
	
}
